package com.xiaoy.core.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.springside.modules.orm.Page;

import com.xiaoy.core.tag.html.GridColumnHtml;
import com.xiaoy.core.tag.html.GridDataHtml;

public class GridDataTag extends BodyTagSupport {
	
	private GridDataHtml gridDataHtml;
	private List<GridColumnHtml>columnHtmls;
	
	
	@Override
	public  int doStartTag() throws JspException {
		
		try {
			initParameters();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return EVAL_BODY_INCLUDE;
	}

	private void initParameters() throws IOException {

		columnHtmls = new ArrayList<GridColumnHtml>();
		gridDataHtml = new GridDataHtml(this);
		
		initList();
		
		
		
//		if (list instanceof Collection) {
//			dataList = (List) list;
//			JspWriter out = pageContext.getOut();
//			Iterator it = dataList.iterator();
//			while(it.hasNext()) {
//				Object obj = it.next();
//				out.write("-->"+obj+"<br/>");
//			}
//		}
	}
	
	@Override
	public int doEndTag() throws JspException {

		JspWriter out = pageContext.getOut();
		try {
			out.write(gridDataHtml.init()+"<br/>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return super.doEndTag();
	}
	
	private void initList() {
		
		if (list instanceof String) {
			list = lookup(list.toString());
			if (list instanceof Page) {
				
				page = (Page<?>) list;
			}
		}else if (list instanceof Page) {
			page = (Page<?>) list;
			
		}else if (list instanceof Collection) {
			dataList = (List) list;
		}
//		else if (list instanceof Collection){
//			
//		}

	}
	
	private Object lookup(String name) {
//		pageContext.getRequest()
		return pageContext.getRequest().getAttribute(name);
	}
	
	
    public void addColumnHtmls (GridColumnHtml html) {
    	columnHtmls.add(html);
    }
    public List<GridColumnHtml> getcolumnHtmls () {
    	return columnHtmls;
    }
	
	//------------------------------------
	private Object list;
	
	private int pagesize;
	private int start;
	private int end;
	private Page<?> page ;
	private String url;
	private List dataList;
	
	public void setList(Object value) {
		if (value instanceof String) {

			this.list = (String) value;
			
		} else {

			this.list = value;
		}
	}

	public Page<?> getPage () {
		return this.page;
	}
	public int getPagesize() {
		return pagesize;
	}

	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public Object getList() {
		return list;
	}
	
	public boolean isEmpty () {
		
		if (dataList==null||dataList.size()==0){
			return true;
		}
		return false;
	}

	public List getDataList() {
		return dataList;
	}

	public void setDataList(List dataList) {
		this.dataList = dataList;
	}

	public GridDataHtml getGridDataHtml() {
		return gridDataHtml;
	}

	public void setGridDataHtml(GridDataHtml gridDataHtml) {
		this.gridDataHtml = gridDataHtml;
	}

	public String getUrl() {
		if(url==null){
			url = pageContext.getServletContext().getContextPath();
		}
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	
	
}
